﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BE.DataFramework;
using BE;

namespace BC.DataFramework
{
    public class ConnectionsBC
    {
        public ConnectionsBE Create(ConnectionsBE item)
        {
            try
            {
                if (item == null)
                {
                    throw new BusinessException("Faltan datos.");
                }
                else if (item.iConnectorId == null)
                {
                    throw new BusinessException("El conector es requerido.");
                }
                else if (item.iConnectorId < 1)
                {
                    throw new BusinessException("El conector es requerido.");
                }
                else if (string.IsNullOrEmpty(item.sConnectionName))
                {
                    throw new BusinessException("El nombre de la conexión es requerido.");
                }
                else if (string.IsNullOrEmpty(item.sConnectionString))
                {
                    throw new BusinessException("La cadena de conexión es requerido.");
                }

                return item.Create();
            }
            catch (Exception ex)
            {
                throw new AppException("ConnectionsBC - Create - ", ex);
            }
        }
        public ConnectionsBE Update(ConnectionsBE item)
        {
            try
            {
                if (item == null)
                {
                    throw new BusinessException("Faltan datos.");
                }
                else if (item.iConnectionId == null)
                {
                    throw new AppException("La conexión es requerida.");
                }
                else if (item.iConnectionId < 1)
                {
                    throw new AppException("La conexión es requerida.");
                }
                else if (item.iConnectorId == null)
                {
                    throw new BusinessException("El conector es requerido.");
                }
                else if (item.iConnectorId < 1)
                {
                    throw new BusinessException("El conector es requerido.");
                }
                else if (string.IsNullOrEmpty(item.sConnectionName))
                {
                    throw new BusinessException("El nombre de la conexión es requerido.");
                }
                else if (string.IsNullOrEmpty(item.sConnectionString))
                {
                    throw new BusinessException("La cadena de conexión es requerido.");
                }

                return item.Update();
            }
            catch (Exception ex)
            {
                throw new AppException("ConnectionsBC - Update - ", ex);
            }
        }
        public bool Delete(ConnectionsBE item)
        {
            try
            {
                if (item == null)
                {
                    throw new BusinessException("Faltan datos.");
                }
                else if (item.iConnectionId == null)
                {
                    throw new AppException("La conexión es requerida.");
                }
               
                return item.Delete() > 0 ? true:false;
            }
            catch (Exception ex)
            {
                throw new AppException("ConnectionsBC - Delete - ", ex);
            }
        }
        public List<ConnectionsBE> Select(ConnectionsBE item)
        {
            try
            {
                return item.Select();
            }
            catch (Exception ex)
            {
                throw new AppException("ConnectionsBC - Select - ", ex);            
            }

        }
    }
}
